package com.eastfair.questionnaire.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.echo.core.EchoService;
import com.eastfair.questionnaire.dto.*;
import com.eastfair.questionnaire.entity.Question;
import com.eastfair.questionnaire.service.QuestionService;
import com.eastfair.questionnaire.vo.QuestionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 问题
 * </p>
 *
 * @author yuwx
 * @date 2021-08-18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/question")
@Api(value = "Question", tags = "问题")
public class QuestionController extends SuperController<QuestionService, Long, Question, QuestionPageQuery, QuestionSaveDTO, QuestionUpdateDTO> {

    private EchoService echoService;
    private QuestionService questionService;
    @Autowired
    public QuestionController(EchoService echoService, QuestionService questionService) {
        this.echoService = echoService;
        this.questionService = questionService;
    }

    @Override
    public void handlerResult(IPage<Question> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Question> questionList = list.stream().map((map) -> {
            Question question = Question.builder().build();
            //TODO 请在这里完成转换
            return question;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(questionList));
    }
    @ApiOperation(value = "根据问卷Id查询所有问题", notes = "根据问卷Id查询所有问题")
    @PostMapping("/listByQuestionnaireId")
    public R<List<QuestionVO>> listByQuestionnaireId(@RequestBody QuestionDTO question) {
        Long projectId = ContextUtil.getProjectId();
        Long questionnaireId = question.getQuestionnaireId();
        if (Objects.isNull(projectId)
                || Objects.isNull(questionnaireId)) {
            return R.fail(ExceptionCode.BASE_VALID_PARAM);
        }
        List<QuestionVO> questionList = questionService.list(projectId, questionnaireId);
        return R.success(questionList);
    }

    @ApiOperation(value = "根据问卷Id查询所有问题以及选中的答案（只显示末级选中的答案）", notes = "根据问卷Id查询所有问题以及选中的答案（只显示末级选中的答案）")
    @PostMapping("/listWithSelectedAnswer")
    public R<List<QuestionVO>> listWithSelectedAnswer(@Validated({QuestionQueryDTO.listWithSelectedAnswer.class}) @RequestBody QuestionQueryDTO question) {
        Long projectId = ContextUtil.getProjectId();
        Long questionnaireId = question.getQuestionnaireId();
        if (Objects.isNull(projectId)
                || Objects.isNull(questionnaireId) ) {
            return R.fail(ExceptionCode.BASE_VALID_PARAM);
        }
        List<QuestionVO> questionList = questionService.listWithSelectedAnswer(projectId, questionnaireId, question.getSubjectType(), question.getSubjectId());
        return R.success(questionList);
    }
    @ApiOperation(value = "保存问题", notes = "保存问题")
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.PUT})
    public R<Boolean> save(
            @Validated({QuestionDTO.validateBase.class, QuestionDTO.validateSave.class})
            @RequestBody QuestionDTO questionDTO) {
        Long projectId = ContextUtil.getProjectId();
        questionDTO.setProjectId(projectId);
        questionService.save( questionDTO);
        return R.successDef();
    }
}
